package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.AddressListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.AddressBean;
import com.lc.luzhengtang.bean.MyCommenBean;
import com.lc.luzhengtang.conn.ConnAddressList;
import com.lc.luzhengtang.conn.ConnDefaultddress;
import com.lc.luzhengtang.event.EventBus;
import com.lc.luzhengtang.event.Info;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class AddressManageActivity2 extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.btn_add_address)
    Button btnAddAddress;
    AddressListAdapter indexAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addressmanage);
        ButterKnife.bind(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        // getAddresData();
        initData();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        indexAdapter = new AddressListAdapter(null);
        indexAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        indexAdapter.isFirstOnly(false);
        recyclerView.setAdapter(indexAdapter);
        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    @OnClick({R.id.img_news, R.id.btn_add_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.btn_add_address:
                startActivity(new Intent(AddressManageActivity2.this, AddAddress.class));
                break;
        }
    }

    @Override
    public void onRefresh() {
        new ConnAddressList(BaseApplication.basePreferences.readUid(), new AsyCallBack<AddressBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, final AddressBean addressBean) throws Exception {
                super.onSuccess(toast, type, o, addressBean);
                List<AddressBean.DataBean> results = addressBean.getData();
                if (results != null) {
                    //  currentPage = 1;
                    indexAdapter.setNewData(results);
                }

                indexAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {


                        switch (view.getId()) {
                            case R.id.img_changeAddress:
                                Intent intent = new Intent();
                                intent.putExtra("address_id", addressBean.getData().get(position).getId() + "");
                                intent.putExtra("status", addressBean.getData().get(position).getStatus() + "");
                                intent.putExtra("shouhuoname", addressBean.getData().get(position).getUsername());
                                intent.putExtra("phone", addressBean.getData().get(position).getMobile());

                                intent.putExtra("address", addressBean.getData().get(position).getAddress());
                                intent.putExtra("detail", addressBean.getData().get(position).getCity());
                                intent.setClass(AddressManageActivity2.this, AddressChange.class);
                                startActivity(intent);
                                break;

                            case R.id.default_address:

                                new ConnDefaultddress(BaseApplication.basePreferences.readUid(), 1, addressBean.getData().get(position).getId(),
                                        new AsyCallBack<MyCommenBean>() {
                                            @Override
                                            public void onStart(int type) throws Exception {
                                                super.onStart(type);
                                            }

                                            @Override
                                            public void onSuccess(String toast, int type, Object o, MyCommenBean myCommenBean) throws Exception {
                                                super.onSuccess(toast, type, o, myCommenBean);
                                                //  Toast.makeText(AddressManageActivity.this, "设置默认地址成功", Toast.LENGTH_SHORT).show();
                                                LovelyToast.makeText(AddressManageActivity2.this, myCommenBean.getMessage(), 0, LovelyToast
                                                        .SUCCESS, LovelyToast.SCALE);
                                                addressBean.getData().get(position).setStatus(1);
                                                initData();
                                            }

                                            @Override
                                            public void onFail(String toast, int type) throws Exception {
                                                super.onFail(toast, type);
                                            }

                                            @Override
                                            public void onEnd(String toast, int type) throws Exception {
                                                super.onEnd(toast, type);
                                            }
                                        }

                                ).execute(AddressManageActivity2.this, false);

                                break;

                            case R.id.layout_item_recycle:

                                String message =addressBean.getData().get(position).getUsername();
//                EventBus.getDefault().post(message);
                                EventBus.getInstance().post(new Info(addressBean.getData().get(position).getUsername(),addressBean.getData().get(position).getAddress(),addressBean.getData().get(position).getMobile(),addressBean.getData().get(position).getCity()));
                                finish();

                                break;


                        }


                    }
                });


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
                setRefreshing(false);
            }
        }


        ).execute(AddressManageActivity2.this, false);
    }


}
